﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Input;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Style/Css/Normal.css")]//用于设计的时候加载样式
    public class PageControl : PageControlBase
    {
        Button btnFirst;
        Button btnPrevious;
        Button btnNext;
        Button btnEnd;
        Button btnToPage;
        StackPanel BtnControls;
        TextBox txtPage;
        //模板定义
        protected override void InitializeComponent()
        {
            Children.Add(new StackPanel
            {
                Name = "BtnControls",
                PresenterFor = this,
                Orientation = Orientation.Horizontal,
                Children =
                {
                    new Button
                    {
                        Name="btnFirst",
                        BorderFill=Color.FromRgb(223,223,223),
                        PresenterFor=this,
                        Content="<<",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="btnPrevious",
                        BorderFill=Color.FromRgb(223,223,223),
                        PresenterFor=this,
                        MarginLeft=2,
                        Content="<",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="p1",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="1",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="p2",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="2",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="p3",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="3",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="p4",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="4",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="p5",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="5",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="p6",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="6",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="p7",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="7",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="p8",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="8",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="p9",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="9",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="btnNext",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content=">",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="btnEnd",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content=">>",
                        Width=30,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Panel
                    {
                        MarginLeft=2,
                        Height=30,
                        BorderFill=Color.FromRgb(223,223,223),
                        Children =
                        {
                            new TextBox
                            {
                                Name="txtPage",
                                PresenterFor=this,
                                Text="1",
                                Width=60,
                                MarginLeft=2,
                                MarginRight=2,
                                TextAlignment=TextAlignment.Center,
                                Classes={
                                    "PageControlButton"
                                }
                            }
                        },
                        Classes={
                            "PageControlButton"
                        }
                    },
                    new Button
                    {
                        Name="btnToPage",
                        PresenterFor=this,
                        BorderFill=Color.FromRgb(223,223,223),
                        MarginLeft=2,
                        Content="跳转",
                        Width=60,
                        Height=30,
                        Classes={
                            "PageControlButton"
                        }
                    },
                }
            });
            Panel b;
            
        }

#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();
            BtnControls = FindPresenterByName<StackPanel>("BtnControls");
            btnFirst = FindPresenterByName<Button>("btnFirst");
            btnPrevious = FindPresenterByName<Button>("btnPrevious");
            btnNext = FindPresenterByName<Button>("btnNext");
            btnEnd = FindPresenterByName<Button>("btnEnd");
            btnToPage = FindPresenterByName<Button>("btnToPage");
            txtPage = FindPresenterByName<TextBox>("txtPage");
           
            for (int i = 1; i < 10; i++)
            {
                Button btn = FindPresenterByName<Button>("p"+i);
                btn.Click += Btn_Click;
            }
            btnFirst.Click += BtnFirst_Click;
            btnPrevious.Click += BtnPrevious_Click;
            btnNext.Click += BtnNext_Click;
            btnEnd.Click += BtnEnd_Click;
            btnToPage.Click += BtnToPage_Click;
            txtPage.KeyDown += TxtPage_KeyDown;
            ReloadPage();
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            PageIndex = int.Parse((sender as Button).Content.ToString());
            StartIndex = (PageIndex - 1) * PageSize;
            ReloadPage();

            PageControlDataChanged?.Invoke(this, new PageControlEventArgs(PageIndex));
        }

        private void TxtPage_KeyDown(object sender, CPF.Input.KeyEventArgs e)
        {
            if (e.KeyCode == (int)Keys.Enter)
            {
                BtnToPage_Click(null, null);
                txtPage.Text = "";
            }
        }

        private void BtnToPage_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPage.Text))
            {
                PageIndex = int.Parse(txtPage.Text);
                StartIndex = (PageIndex - 1) * PageSize;
                ReloadPage();

                PageControlDataChanged?.Invoke(this, new PageControlEventArgs(PageIndex));
            }
        }

        private void BtnEnd_Click(object sender, RoutedEventArgs e)
        {
            EndPage();
        }

        private void BtnNext_Click(object sender, RoutedEventArgs e)
        {
            NextPage();
        }

        private void BtnPrevious_Click(object sender, RoutedEventArgs e)
        {
            PreviousPage();
        }

        private void BtnFirst_Click(object sender, RoutedEventArgs e)
        {
            FirstPage();
        }

        /// <summary>
        /// Occurs when [show source changed].
        /// </summary>
        public override event EventHandler<PageControlEventArgs> PageControlDataChanged;
        /// <summary>
        /// 关联的数据源
        /// </summary>
        /// <value>The data source.</value>
        public override List<object> DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                if (value == null)
                {
                    base.DataSource = new List<object>();
                }
                else
                {
                    base.DataSource = value;

                }
                PageIndex = 1;
                ResetPageCount();
                var s = GetCurrentSource();
            }
        }
        /// <summary>
        /// 每页显示数量
        /// </summary>
        /// <value>The size of the page.</value>
        public  int PageSize
        {
            get
            {
                return base.PageSize;
            }
            set
            {
                base.PageSize = value;

                PageControlDataChanged?.Invoke(this, new PageControlEventArgs(PageIndex));
            }
        }
        public override int PageCount
        {
            get
            {
                return base.PageCount;
            }
            set
            {
                base.PageCount = value;
                ReloadPage();
            }
        }


        public override int PageIndex
        {
            get
            {
                return base.PageIndex;
            }
            set
            {
                base.PageIndex = value;
                ReloadPage();
            }
        }

        /// <summary>
        /// 第一页
        /// </summary>
        public override void FirstPage()
        {
            if (PageIndex == 1)
                return;
            PageIndex = 1;
            ReloadPage();

            PageControlDataChanged?.Invoke(this, new PageControlEventArgs(PageIndex));
        }

        /// <summary>
        /// 上一页
        /// </summary>
        public override void PreviousPage()
        {
            if (PageIndex <= 1)
            {
                return;
            }
            PageIndex--;
            ReloadPage();

            PageControlDataChanged?.Invoke(this, new PageControlEventArgs(PageIndex));
        }

        /// <summary>
        /// 下一页
        /// </summary>
        public override void NextPage()
        {
            if (PageIndex >= PageCount)
            {
                return;
            }
            PageIndex++;
            ReloadPage();

            PageControlDataChanged?.Invoke(this, new PageControlEventArgs(PageIndex));
        }

        /// <summary>
        /// 最后一页
        /// </summary>
        public override void EndPage()
        {
            if (PageIndex == PageCount)
                return;
            PageIndex = PageCount;
            ReloadPage();

            PageControlDataChanged?.Invoke(this, new PageControlEventArgs(PageIndex));
        }

        /// <summary>
        /// Resets the page count.
        /// </summary>
        private void ResetPageCount()
        {
            if (PageSize > 0)
            {
                if (base.DataSource != null)
                    PageCount = base.DataSource.Count / base.PageSize + (base.DataSource.Count % base.PageSize > 0 ? 1 : 0);
            }
            ReloadPage();
        }

        /// <summary>
        /// Reloads the page.
        /// </summary>
        private void ReloadPage()
        {
            try
            {
                List<int> lst = new List<int>();

                if (PageCount <= 9)
                {
                    for (var i = 1; i <= PageCount; i++)
                    {
                        lst.Add(i);
                    }
                }
                else
                {
                    if (this.PageIndex <= 6)
                    {
                        for (var i = 1; i <= 7; i++)
                        {
                            lst.Add(i);
                        }

                        lst.Add(-1);
                        lst.Add(PageCount);
                    }
                    else if (this.PageIndex > PageCount - 6)
                    {
                        lst.Add(1);
                        lst.Add(-1);
                        for (var i = PageCount - 6; i <= PageCount; i++)
                        {
                            lst.Add(i);
                        }
                    }
                    else
                    {
                        lst.Add(1);
                        lst.Add(-1);
                        var begin = PageIndex - 2;
                        var end = PageIndex + 2;
                        if (end > PageCount)
                        {
                            end = PageCount;
                            begin = end - 4;
                            if (PageIndex - begin < 2)
                            {
                                begin = begin - 1;
                            }
                        }
                        else if (end + 1 == PageCount)
                        {
                            end = PageCount;
                        }

                        for (var i = begin; i <= end; i++)
                        {
                            lst.Add(i);
                        }

                        if (end != PageCount)
                        {
                            lst.Add(-1);
                            lst.Add(PageCount);
                        }
                    }
                }

                //btnFirst.Radius = btnPrevious.Radius =
                //    btnNext.Radius = btnEnd.Radius = btnTxt.Radius = btnToPage.Radius = PageControlRadius;
                for (int i = 0; i < 9; i++)
                {

                    Button c = FindPresenterByName<Button>("p"+(i+1));
                    if (c == null)
                    {
                        continue;
                    }
                    //c.Radius = PageControlRadius;
                    if (i >= lst.Count)
                    {
                        c.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        if (lst[i] == -1)
                        {
                            c.Content = "...";
                            c.IsEnabled = false;
                        }
                        else
                        {
                            c.Content = lst[i].ToString();
                            c.IsEnabled = true;
                        }

                        c.Visibility = Visibility.Visible;
                        if (lst[i] == PageIndex)
                        {
                            c.BorderFill = _pageControlSelectedBorderColor;
                        }
                        else
                        {
                            c.BorderFill = _pageControlNormalBorderColor;
                        }
                    }
                }

                ShowBtn(PageIndex > 1, PageIndex < PageCount);
            }
            catch
            {
            }
        }
        private Color _pageControlNormalBorderColor = Color.FromArgb(90, 223, 223, 223);
        private Color _pageControlSelectedBorderColor = Color.FromRgb(255, 77, 59);
        /// <summary>
        /// 控制按钮显示
        /// </summary>
        /// <param name="blnLeftBtn">是否显示上一页，第一页</param>
        /// <param name="blnRightBtn">是否显示下一页，最后一页</param>
        protected override void ShowBtn(bool blnLeftBtn, bool blnRightBtn)
        {
            if(btnFirst==null|| btnPrevious == null||btnNext == null || btnEnd == null)
            {
                return;
            }
            btnFirst.IsEnabled = btnPrevious.IsEnabled = blnLeftBtn;
            btnNext.IsEnabled = btnEnd.IsEnabled = blnRightBtn;
        }
        //用户代码

#endif
    }
}
